package com.zyd.work.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zyd.common.AjaxUtils;
import com.zyd.common.CommonController;
import com.zyd.common.DateTools;
import com.zyd.work.domain.vo.MonthLog;
import com.zyd.work.logic.IMonthLogFacade;
import com.zyd.xtgl.domain.vo.TbAdmUser;

@Controller
@RequestMapping("/monthlog")
public class MonthLogAction extends CommonController {
	private IMonthLogFacade monthLogImpl;

	@Resource(name="monthLogImpl")
	public void setMonthLogImpl(IMonthLogFacade monthLogImpl) {
		this.monthLogImpl = monthLogImpl;
	}
	/**
	 * 页面传过来的String转换成Date类型
	 * 
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat monthformat = new SimpleDateFormat("yyyy-MM");
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class,"monthofyear", new CustomDateEditor(monthformat,
				true));
		binder.registerCustomEditor(Date.class,"commitdate", new CustomDateEditor(dateformat,
				true));
	}
	/**
	 * 转向到月报主页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=toMonthLog")
	public ModelAndView toWorkLog(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("worklog/mymonthlog");
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @param monthlog
	 * @param result
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addMonthLog")
	public String addMonthLog(HttpServletRequest request,
			HttpServletResponse response,@Valid MonthLog monthlog,BindingResult result) throws IOException{
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		String monthofyear = request.getParameter("monthofyear");
		List<MonthLog> list =	monthLogImpl.getMonthLogByMonthofyear(monthofyear);
		if(result.hasErrors()){
			out.write("输入信息有误，请检查~~~~");
		}else if(list.size()>0){
			out.write("本月月报已经存在~~~~");
		}else{
			monthlog.setMonthofyear(DateTools.stringToMonth(monthofyear));
			monthlog.setUserid(user.getUserid());
			monthLogImpl.addMonthLog(monthlog);
			out.write("添加月报成功~~~~");
		}
		out.close();
		return null;
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @param monthlog
	 * @param result
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateMonthLog")
	public String updateMonthLog(HttpServletRequest request,
			HttpServletResponse response,@Valid MonthLog monthlog,BindingResult result) throws IOException{
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(result.hasErrors()){
			out.write("输入信息有误，请检查~~~~");
		}else{
			monthLogImpl.updateMonthLog(monthlog);
			out.write("添加月报成功~~~~");
		}
		out.close();
		return null;
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getMonthLogByid")
	public String  getMonthLogByid(HttpServletRequest request,
			HttpServletResponse response){
		String id = request.getParameter("id");
		MonthLog monthlog =	monthLogImpl.getMonthLogById(id);
		return AjaxUtils.ajaxList(request, response, monthlog);
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getNowDateMonthLogList")
	public String getNowDateMonthLogList(HttpServletRequest request,
			HttpServletResponse response){
		String monthofyear = request.getParameter("nowmonth");
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		List<MonthLog> list = monthLogImpl.getMonthLogByUserid(monthofyear, user.getUserid());
		return AjaxUtils.ajaxList(request, response, list);
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=searchMonthLogList")
	public String searchMonthLogList(HttpServletRequest request,
			HttpServletResponse response){
		String startmonth = request.getParameter("startmonth");
		String endmonth = request.getParameter("endmonth");
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		List<MonthLog> list = monthLogImpl.searchMonthLogByUserid(startmonth, endmonth, user.getUserid());
		return AjaxUtils.ajaxList(request, response, list);
	}
	
}
